package com.business_clean.ui.activity.group;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.callback.OnDialogConfirmListener;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.app.weight.EqualSpacingItemDecoration;
import com.business_clean.data.mode.group.GroupMangerEntity;
import com.business_clean.databinding.ActivityGroupManagerBinding;
import com.business_clean.ui.adapter.group.GroupMangerAdapter;
import com.business_clean.viewmodel.request.GroupViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class GroupManagerActivity extends BaseActivity<GroupViewModel, ActivityGroupManagerBinding> {

    private int page = 1;
    private int requestType;
    private GroupMangerAdapter mAdapter;
    private boolean isSelected = false;
    private String project_uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            isSelected = getIntent().getExtras().getBoolean("isSelected");
            project_uuid = getIntent().getExtras().getString("project_uuid");
            
            String project_name = getIntent().getExtras().getString("project_name");
            if (!TextUtils.isEmpty(project_name)) {
                mDatabind.tabbar.setTitle(project_name + "的小组");
            } else {
                mDatabind.tabbar.setTitle("选择小组");
            }
        } else {
            if (App.appViewModelInstance.getProjectInfo().getValue() != null) {
                mDatabind.tabbar.setTitle(App.appViewModelInstance.getProjectInfo().getValue().getProject_short_name() + "的小组");
            }
        }
        mAdapter = new GroupMangerAdapter(isSelected);
        mDatabind.list.recyclerView.setAdapter(mAdapter);
        mDatabind.list.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.list.recyclerView.addItemDecoration(new EqualSpacingItemDecoration(SizeUtils.dp2px(10)));
        mDatabind.bottom.butBase.setText("+ 添加小组");
        getMDatabind().list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });
        requestOne();
    }

    @Override
    public void initObserver() {
        App.appViewModelInstance.getRefreshGroupManagerList().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                requestOne();
            }
        });
    }

    @Override
    public void onBindViewClick() {
        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityForwardUtil.startActivity(AddGroupActivity.class);
            }
        });

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_item_group_del:
                        CommonUtils.showGeneralDialog(GroupManagerActivity.this, "删除小组", "是否删除小组？",
                                "取消", "确定", null, new OnDialogConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        mViewModel.requestDelGroup(mAdapter.getData().get(position).getUuid());
                                    }
                                });
                        break;
                    case R.id.iv_item_group_edit:
                        Bundle bundle = new Bundle();
                        bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                        ActivityForwardUtil.startActivity(AddGroupActivity.class, bundle);
                        break;
                }
            }
        });

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (isSelected) {
                    App.appViewModelInstance.getSelectedGroup().setValue(mAdapter.getData().get(position));
                    finish();
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                    ActivityForwardUtil.startActivity(AddGroupActivity.class, bundle);
                }
            }
        });
    }


    @Override
    public void onRequestSuccess() {
        mViewModel.getGroupMangerEntity().observe(this, new Observer<GroupMangerEntity>() {
            @Override
            public void onChanged(GroupMangerEntity contactMangerEntity) {
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(contactMangerEntity.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(contactMangerEntity.getList());
                        }
                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(contactMangerEntity.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(contactMangerEntity.getList());
                        }
                        break;
                }
            }
        });

        //执行了操作，然后刷新界面
        mViewModel.getDelGroup().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                requestOne();
            }
        });
    }

    void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        mViewModel.requestGroupAll(page, project_uuid);
    }

    void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        mViewModel.requestGroupAll(page, project_uuid);
    }
}